package com.seele.tigerwallet.ui.manager;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.ui.callback.OnFinishListener;
import com.seele.tigerwallet.ui.utils.BaseDialog;
import com.seele.tigerwallet.ui.utils.KeyBoardUtils;
import com.seele.tigerwallet.ui.views.KeyboardAdapter;
import com.seele.tigerwallet.ui.views.KeyboardView;
import com.seele.tigerwallet.ui.views.PasswordInputView;

import java.util.List;

public class KeyDialogManager implements KeyboardAdapter.OnKeyboardClickListener {

    private Context mContext;
    private BaseDialog mBaseDialog;

    private PasswordInputView mPasswordInputView;

    private List<String> mKeys;

    private KeyboardView mKeyboardView;

    private OnFinishListener mOnFinishListener;
    private StringBuilder mStringBuilder;

    public int mLength;

    public KeyDialogManager(Context context, int lenght) {
        this.mContext = context;
        this.mLength = lenght;
    }

    public void show() {
        if (null != mBaseDialog) {
            if (!mKeyboardView.isVisible()) {
                mKeyboardView.setVisibility(View.VISIBLE);
            }
            mBaseDialog.show();
        }
    }

    public void setOnFinishListener(OnFinishListener baseListener) {
        mOnFinishListener = baseListener;


    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.ll_close:
                    dismiss();

            }

        }
    };

    public void dismiss() {
        if (null != mBaseDialog && mBaseDialog.isShowing()) {
            mBaseDialog.dismiss();
            if (null != mStringBuilder) {
                int length = mStringBuilder.length();
                for (int i = length - 1; i >= 0; i--) {
                    mStringBuilder.deleteCharAt(i);
                }
                mPasswordInputView.setKeyText(mStringBuilder);
            }
        }
    }

    /**
     * 初始化Dialo
     */
    public KeyDialogManager initKeyDialog() {

        if (null == mBaseDialog) {
            mBaseDialog = new BaseDialog.Builder(mContext).view(R.layout.dialog_key)
                    .setDialogPosition(Gravity.CENTER) //设置dialog显示位置
                    .cancelTouchout(false)//点击dialog背景不能取消dialog，默认为true
                    .setWidthPX(Integer.MAX_VALUE)
                    .addViewOnclick(R.id.ll_close, onClickListener)
                    .build();

        }
        mPasswordInputView = mBaseDialog.findView(R.id.password_view);
        mKeyboardView = mBaseDialog.findView(R.id.keyboard_view);
        mKeyboardView.setOnKeyBoardClickListener(this);
        mKeys = mKeyboardView.getDatas();
        mPasswordInputView.setOnFinishListener(new OnFinishListener() {
            @Override
            public void onPasswordFinished(CharSequence text) {
                if (null != mOnFinishListener) {
                    if (text.length() == mLength) {
                        mOnFinishListener.onPasswordFinished(text);
                    }
                }

            }
        });

        return this;
    }



    @Override
    public void onKeyClick(View view, RecyclerView.ViewHolder holder, int position) {
        switch (position) {
            case 9: // 按下小数点
                break;
            default: // 按下数字键
                if (null == mStringBuilder) {
                    mStringBuilder = new StringBuilder();
                }
                if (mStringBuilder.length() >= mLength) {
                    return;
                }
                mStringBuilder.append(mKeys.get(position));
                mPasswordInputView.setKeyText(mStringBuilder.toString());
                break;
        }


    }


    @Override
    public void onDeleteClick(View view, RecyclerView.ViewHolder holder, int position) {
        // 点击删除按钮
        if (null != mStringBuilder) {
            int length = mStringBuilder.length() - 1;
            if (length > 0) {
                mStringBuilder.deleteCharAt(length);
            } else if (length == 0) {
                mStringBuilder.deleteCharAt(0);
            }
            mPasswordInputView.setKeyText(mStringBuilder.toString());
        }

    }


    public PasswordInputView getInputView(){
        return mPasswordInputView;
    }
    /**
     * 保存键盘
     */
    public void hideKeyboard() {
        if (null != mKeyboardView) {
            mKeyboardView.setVisibility(View.INVISIBLE);
        }
    }

    public void closeKeybord(){
        KeyBoardUtils.closeKeybord(mPasswordInputView,mContext);
    }
}

